#include"Game.h"

/////////////////
//include
/////////////////
#include<ANX\InputElement.h>
#include<ANX\FPS.h>
#include<ANX\TextureManager.h>
#include<ANX\ANX.h>
#include<ANX\Key.h>
#include<ANX\Mouse.h>
#include"Title.h"
#include"GameMain.h"

/////////////////
//prottype
/////////////////

/////////////////
//const
/////////////////

FPS fps;
Game::Game(HWND hwnd)
{
	ANX::CreateDeviceAndSwapChain(hwnd,&swap_chain,&device,&device_context);
	render_target = ANX::CreateRenderTargetView(&swap_chain,&device);
	depth = NULL;
	depth = device.CreateDepthStencilView();
	device_context.SetRenderTarget(1,render_target,depth);
	texture_manager						= new TextureManager(&device);
	sprite_drawer						= new SpriteDrawer(&device,&device_context);
	scenes[Scenes::Title]				= new Title();
	scenes[Scenes::GameMain]			= new GameMain();
	now_scene_type = Scenes::GameMain;
	camera = Camera(&device_context,&device,Vector3D(-100,10,0),Vector3D(0,10,0));
	ModelManager::SetDevice(&device);
	for(int i = 0;i<Scenes::Num;i++)
	{
		scenes[i]->Init();
		scenes[i]->ContentLoad(&device);
	}
	pri_time = 0;
	Mouse::SetWinHnd(hwnd);
}
Game::~Game()
{

}
void Game::Run()
{
	texture = device.CreateTexture2D(WINDOW_X,WINDOW_Y,DXGI_FORMAT_R32G32B32A32_FLOAT,D3D11_BIND_SHADER_RESOURCE,D3D11_USAGE_DYNAMIC,D3D11_CPU_ACCESS_WRITE);
	ID3D11ShaderResourceView* shader_resouce = device.CreateShaderResource(texture);

	while(!CheckMessarge())
	{
		if(!CheckFrame())
			continue;
		device_context.ClearRenderTarget(render_target);
		device_context.ClearDepth(depth);
		Key::Update();
		Mouse::Update();
		camera.Update();
		camera.SetMatrix();
		sprite_drawer->Begin();
		shader_resouce = device.CreateShaderResource(texture);
		scenes[now_scene_type]->Update();
		scenes[now_scene_type]->Draw(&device_context);
		sprite_drawer->End();
		//fps.Draw();
		swap_chain.Present();
	}

}
bool Game::CheckMessarge()
{
	MSG msg;
	if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
		if(msg.message == WM_QUIT)
		{
			return true;
		}
	}
	return false;
}
void Game::SetScene(Scenes::Scene scene_type)
{
	now_scene_type = scene_type;
	scenes[now_scene_type]->Init();
}
bool Game::CheckFrame()
{
	clock_t tmp_time =  clock();
	if( (double)((tmp_time - pri_time))/CLOCKS_PER_SEC > 1/60.0)
	{
		pri_time = tmp_time;
		return true;
	}
	return false;

}
Scenes::Scene Game::now_scene_type;
Scene* Game::scenes[Scenes::Num];
Camera Game::camera;
SpriteDrawer* Game::sprite_drawer;